#include <string>
#include "RtspServer.h"
#include "ProtocolApi.h"
#include "StreamApi.h"

using namespace Protocol;
using namespace Stream;
using namespace std;

RtspServer *RtspServer::s_rtspServer = nullptr;
mutex RtspServer::sm_mutex;


void RtspServerInit(){
	RtspServer::instance();
}

RtspServer::RtspServer(){
	RegistRPC();
}

void RtspServer::RegistRPC(){
	RPCMethodMap method_map;
	method_map.emplace("setRtspAuth", CRPCFuntion::CreateRpc([this](const Json::Value &request, const BufferMap::Ptr &inMap, Json::Value &response, BufferMap::Ptr &outMap){
		return setRtspAuth(request, inMap, response, outMap);
	}));

	IProtocolInterface::GetInstance()->RegistProtocol("Rtsp", method_map);
}


bool RtspServer::setRtspAuth(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outMap){
	//tracef("Rtsp request -------------------->  %s",request.toStyledString().c_str());
	//获取设置的用户名和密码
	string username;
	string password;
	bool iRet1;
	bool iRet2;
	iRet1 =  Json::getJsonString(request, "Param.username", username);
	iRet2 =  Json::getJsonString(request, "Param.password", password);
	if(!iRet1 || !iRet2){
		errorf("get  auth username or password error");
		return false;
	}
	//判断用户名和密码是否为空，在返回消息中加以答应
	if(IStreamInterface::GetInstance()->SetRtspAuth((char*)username.c_str() , (char*)password.c_str())){
		response["msg"] = "Succeed to modify username and password"; 
	}else{		
		response["msg"] = "modify username and password is Faild"; 
	}
	return true;
}
